<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 组件--el-tree -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- default-expand-all默认展开所有的节点 -->
        <el-tree
          :data="list"
          default-expand-all
        >
          <!-- 作用域插槽 -->
          <template #default="{data}">
            <!-- 用一个行列布局 -->
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="good" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <!-- 点击控制弹层的展开和收起 -->
                        <!-- 点击添加子部门,id为空传过去的是pid为空,即为一级目录 -->
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hIsEdit(data.id)">编辑部门</el-dropdown-item>
                        <!-- v-if data内children长度为0 就显示删除部门按钮 -->
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- {{ data }} -->
          </template>
        </el-tree>

      </el-card>
      <el-dialog
        :title="isEdit?'编辑部门':'新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <!-- 编辑功能父传子,把isedit的状态传给子组件 -->
        <!-- 可以使用  v-if="showDialog" 使每次点开编辑是对应同步的信息 -->
        <!-- 这里用的是另外一种方法 -->
        <!--b2. 父组件接收close事件 -->
        <addOrEdit
          :id="curId"
          ref="MyAddRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="showDialog=false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import addOrEdit from './deptDialog.vue' // 引入弹层
export default {
  components: { addOrEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false, // 控制添加子部门弹层,默认弹层关闭
      curId: '', // 点击当前部门的id
      isEdit: false, // 只有两种状态,false为新增功能,true为编辑功能
      originList: []

    }
  },
  /*   // 解决数据不更新的方式3 /侦听器
  watch: {
    id: function(newVal, oldVal) {
      console.log('新值', newVal)
      console.log('旧值', oldVal)
      this.loadDepartmentsById()
    }
  }, */
  created() {
    this.loadDepartments()
  },
  methods: {
    // 调用子组件的清空表单
    hClose() {
      this.$refs.MyAddRef.resetForm()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('你确定要删除吗?')
        .then(async() => {
          const res = await delDepartments(id)
          if (res.code !== 10000) return this.$message.error(res.message)
          this.$message.success(res.message)
          console.log('删除部门', res)
          this.loadDepartments()
        })
        .catch(error => console.log(error))
    },
    // 编辑功能
    hIsEdit(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = true // 打开编辑
      // 解决公用组件数据不更新的方式2
      // 可以在父组件中调用子组件中的获取部门详情的数据填充到表单
      this.$nextTick(() => {
        this.$refs.MyAddRef.loadDepartmentsById()
      })
    },
    hSuccess() {
      // 子传父 /重新渲染 关闭弹窗
      this.loadDepartments()
      this.showDialog = false
    },
    // 新增功能
    hAdd(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false // 打开新增
    },

    async loadDepartments() {
      try {
        const res = await getDepartments()
        // 添加 避免同名 同code
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        console.log('查询部门', res)
        // 删除第一条数据
        res.data.depts.shift()
        /*         // 渲染数据
        this.list = res.data.depts */
        // 完成树形结构渲染
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
